package edu.mse.camel.client.posterboard.ui.editpart.policy;

import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import com.cea.papyrus.core.dnd.ObjectTransfer;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;

import edu.mse.camel.client.posterboard.posterboard.PBDiagram;
import edu.mse.camel.client.posterboard.posterboard.Posterboard;
import edu.mse.camel.client.posterboard.ui.commands.PBUMLDiagramCreateCommand;
import edu.mse.camel.client.posterboard.ui.commands.PBUMLDiagramSetConstraintCommand;
import edu.mse.camel.client.posterboard.ui.commands.PBWBDiagramCreateCommand;
import edu.mse.camel.client.posterboard.ui.commands.PBWBDiagramSetConstraintCommand;
import edu.mse.camel.client.posterboard.ui.editor.PosterBoardModelManager;
import edu.mse.camel.client.posterboard.ui.editpart.IPosterBoardDiagramEditpart;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;

public class PosterBoardXYLayoutEditPolicy extends XYLayoutEditPolicy {

	private PosterBoardModelManager pbModelManager;
	private boolean isNewWB;
	

	public PosterBoardXYLayoutEditPolicy(PosterBoardModelManager pbModelManager) {
		this.pbModelManager = pbModelManager;
		this.isNewWB = true;
	}

	/* (non-Javadoc)
	 * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(ChangeBoundsRequest, EditPart, Object)
	 */
	protected Command createChangeConstraintCommand(ChangeBoundsRequest request,
			EditPart child, Object constraint) {
		if (child instanceof IPosterBoardDiagramEditpart && constraint instanceof Rectangle) {
			// return a command that can move and/or resize a Shape
			IPosterBoardDiagramEditpart castedChild = (IPosterBoardDiagramEditpart) child;
			PBDiagram pbDiagram = castedChild.getPbDiagram();
			if (pbDiagram.getDelegate() instanceof WhiteboardDiagram) {
				return new PBWBDiagramSetConstraintCommand(
						pbDiagram, request, (Rectangle) constraint);
			} else if (pbDiagram.getDelegate() instanceof Diagram) {
				return new PBUMLDiagramSetConstraintCommand(
					(IPosterBoardDiagramEditpart) child, 
					request, 
					(Rectangle) constraint);
			}
		}
		return super.createChangeConstraintCommand(request, child, constraint);
	}

	/* (non-Javadoc)
	 * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(EditPart, Object)
	 */
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		// not used in this example
		return null;
	}

	/* (non-Javadoc)
	 * @see LayoutEditPolicy#getCreateCommand(CreateRequest)
	 */
	protected Command getCreateCommand(CreateRequest request) {
		// Currently we only support drag and drop
		// You cannot actually create a new diagram from the posterboard
		List transferData = (List) request.getExtendedData().get(
				ObjectTransfer.UML_TRANSFER_OBJECT);
		if (transferData != null) {
			Object o = transferData.get(0);
			if (o instanceof Diagram) {
				return createUMLCommand(request, (Diagram) o);
			} else if (o instanceof WhiteboardDiagram) {
				return createWhiteboardCommand(request, (WhiteboardDiagram) o);

			}
		}
		return null;
	}


	//TODO TODO
	private Command createWhiteboardCommand(
			CreateRequest request, WhiteboardDiagram wbDiagram) {
		if (isNewWB) {
			isNewWB = false;
			return new PBWBDiagramCreateCommand(
				pbModelManager.getWhiteboard(), 
				(Posterboard) getHost().getModel(), 
				(Rectangle) getConstraintFor(request));
		} else {
			return new PBWBDiagramCreateCommand(
					pbModelManager.getAdditionalWhiteboard(), 
					(Posterboard) getHost().getModel(), 
					(Rectangle) getConstraintFor(request));
		}
	}

	private Command createUMLCommand(CreateRequest request, Diagram diagram) {
		if (diagram != null) {
			return new PBUMLDiagramCreateCommand(
					diagram, 
					(Posterboard) getHost().getModel(), 
					(Rectangle) getConstraintFor(request));
		}
		return null;
	}

}
